{# 1 一键渲染简易表单 #}
<!-- 重构：开始感觉到它的局限性，一些字段并不适合用它来渲染，比如单选框。 -->
{% macro form_field_all(form) %}
    {{ form.csrf_token }}
    {% for key in form.__dict__['_fields'] %}
        {% if ('submit' not in key) and ('csrf_token' not in key) %}
            <div class="form-group">
                {% set field = form.__dict__['_fields'][key] %}
                {{ field.label }}

                <!-- 单选框不被渲染为表单控件 -->
                {% if not isinstance(field, BooleanField) %}
                    {{ field(class='form-control') }}
                {% else %}
                    {{ field() }}
                {% endif %}
                
                {% if field.errors -%}
                    {% for error in field.errors -%}
                        <small class="error">{{ error }}</small><br>
                    {%- endfor %}
                {%- endif %}
            </div>
        {% endif %}
    {% endfor %}
    {{ form.submit(class='btn btn-primary')}}
{% endmacro %}


{# 1.1 渲染一个表单字段 #}
{% macro form_field(field) %}
    {{ field.label }}
    {{ field(class='form-control') }}
    {% if field.errors -%}
        {% for error in field.errors -%}
            <small class="error">{{ error }}</small><br>
        {%- endfor %}
    {%- endif %}
{% endmacro %}


{# 1.2 渲染传入的表单字段列表 #}
{% macro form_field_list(fields, form) %}
    {{ form.csrf_token }}
    {% for field in fields %}
        <div class="form-group">
            {{ form_field(field) }}
        </div>
    {% endfor %}
{% endmacro %}


{# 2 添加一个模态框按钮 - 每个按钮应该提供一个不同的“id”参数 (不能以数字开头) #}
{% macro modal_botton(id, btn_class, btn_name, title, body, footer_href) %}
<button type="button" class="btn {{ btn_class }}" data-toggle="modal" data-target="#{{ id }}">
    {{ btn_name }}
</button>

<div class="modal fade" id="{{ id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">{{ title }}</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                {# 如何让调用者可以定义这段文字的样式呢？ #}
                {{ body }}
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <a class="btn btn-danger btn" href="{{ footer_href }}" role="button">确定</a>
            </div>
        </div>
    </div>
</div>
{% endmacro %}

{# 3 消息闪现-带样式 #}
{% macro flash_messages() %}
    {% for message in get_flashed_messages(with_categories=True) %}
    <div class="row alert alert-{{ message[0] }}" role="alert">
        <button type="button" class="close" data-dismiss="alert">&times;&nbsp;</button>
        {{ message[1] }}
    </div>
    {% endfor %}
{% endmacro %}
